//
//  MineViewController.m
//  IntimateGift
//
//  Created by zh on 14/12/17.
//  Copyright (c) 2014年 蔡宏业. All rights reserved.
//

#import "MineViewController.h"
#import "UIColor+AddColor.h"
#import "LoginViewController.h"
#import "MoreViewController.h"
#import "MMDrawerBarButtonItem.h"
#import "UIViewController+MMDrawerController.h"
#import "DataBaseHandler.h"
#import "BannerGift.h"
#import "Choiceness.h"
#import "AllGroup.h"
#import "UIImageView+WebCache.h"
#import "MineCell.h"
#import "DetailsViewController.h"
#import "Mine.h"
#import "AppDelegate.h"
#import "WBHttpRequest.h"
#import "WBSDKBasicButton.h"
#import "WeiboSDK.h"
#import "WBSDKRelationshipButton.h"
#import "NoCollectionCell.h"

#define DETAIL_HIGHT 0.315 * self.view.bounds.size.height  // 显示图片的高度
#define FINAL_HIGHT 0    // 上拉滑动最终留下的显示图片高度
#define IMAGEVIEW_ORIGIN_Y -0.086 * self.view.bounds.size.height// 图片的实际起点纵坐标
#define IMAGEVIEW_ORIGIN_HEITH 0.7 * self.view.bounds.size.height  // 图片的实际高度
@interface MineViewController () <UITableViewDelegate, UITableViewDataSource, UIAlertViewDelegate, WBHttpRequestDelegate>

@property (nonatomic, retain) UITableView *myTableView;

@property (nonatomic, retain) UIImageView *myImageView;

@property (nonatomic, retain) NSMutableArray *arr;

@property (nonatomic, retain)UISegmentedControl *seg;

@property (nonatomic, assign)NSInteger number;

@property (nonatomic ,retain)UILabel *labelOne;

@property (nonatomic, retain)UILabel *labelTwo;

@property (nonatomic, retain)UILabel *labelThree;

@property (nonatomic, retain) Mine *mine;

@property (nonatomic, retain) UIImageView *weiboImageView;

@property (nonatomic, retain) UILabel *weiboName;

@property (nonatomic, retain) UILabel *attenNum;

@property (nonatomic, retain) UILabel *beanNum;

@property (nonatomic, retain) UILabel *weiboNum;

@property (nonatomic, retain) UIAlertView *myAlertView;



@end

@implementation MineViewController

- (void)dealloc
{
    [_myTableView release];
    [_arr release];
    [_myImageView release];
    [super dealloc];
}


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        //数据库单例方法
        DataBaseHandler *dataHandler = [DataBaseHandler shareInstance];
        [dataHandler  openDB];
        //初始化全局数组，由于segmentedControl起始页selectedSegmentIndex设置为1，为精选项因此从数据出读取精选数组，给刚开始的视图提供数据
        self.arr = [dataHandler selectChoiceness];
        //全局变量number赋初值，tableView返回cell为精选数据cell
        self.number = 1;
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center addObserver:self selector:@selector(receiveNotifica:) name:@"tamenlaile" object:nil];
        NSNotificationCenter *center1 = [NSNotificationCenter defaultCenter];
        [center1 addObserver:self selector:@selector(receiveNotifica1:) name:@"tamenlaile1" object:nil];
        
        
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.alpha = 1;
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBar.hidden = NO;
}

- (void)hide
{
    // 隐藏登陆
    UIImageView *loginImageView = (UIImageView *)[self.view viewWithTag:10001];
    loginImageView.hidden = YES;
    UILabel *label = (UILabel *)[self.view viewWithTag:10002];
    label.hidden = YES;
    UIButton *loginButton = (UIButton *)[self.view viewWithTag:10003];
    loginButton.hidden = YES;
    // 显示个人信息
    self.weiboImageView.hidden = NO;
    self.weiboName.hidden = NO;
    UILabel *attenLabel = (UILabel *)[self.view viewWithTag:10004];
    attenLabel.hidden = NO;
    self.attenNum.hidden = NO;
    UILabel *beanLabel = (UILabel *)[self.view viewWithTag:10005];
    beanLabel.hidden = NO;
    self.beanNum.hidden = NO;
    UILabel *wbLabel = (UILabel *)[self.view viewWithTag:10006];
    wbLabel.hidden = NO;
    self.weiboNum.hidden = NO;
    UILabel *shuGangLabelOne = (UILabel *)[self.view viewWithTag:10007];
    shuGangLabelOne.hidden = NO;
    UILabel *shuGangLabelTwo = (UILabel *)[self.view viewWithTag:10008];
    shuGangLabelTwo.hidden = NO;
    UIButton *cancelButton = (UIButton *)[self.view viewWithTag:10009];
    cancelButton.hidden = NO;
}

- (void)show
{
    // 显示登陆
    UIImageView *loginImageView = (UIImageView *)[self.view viewWithTag:10001];
    loginImageView.hidden = NO;
    UILabel *label = (UILabel *)[self.view viewWithTag:10002];
    label.hidden = NO;
    UIButton *loginButton = (UIButton *)[self.view viewWithTag:10003];
    loginButton.hidden = NO;
    // 隐藏个人信息
    self.weiboImageView.hidden = YES;
    self.weiboName.hidden = YES;
    UILabel *attenLabel = (UILabel *)[self.view viewWithTag:10004];
    attenLabel.hidden = YES;
    self.attenNum.hidden = YES;
    UILabel *beanLabel = (UILabel *)[self.view viewWithTag:10005];
    beanLabel.hidden = YES;
    self.beanNum.hidden = YES;
    UILabel *wbLabel = (UILabel *)[self.view viewWithTag:10006];
    wbLabel.hidden = YES;
    self.weiboNum.hidden = YES;
    UILabel *shuGangLabelOne = (UILabel *)[self.view viewWithTag:10007];
    shuGangLabelOne.hidden = YES;
    UILabel *shuGangLabelTwo = (UILabel *)[self.view viewWithTag:10008];
    shuGangLabelTwo.hidden = YES;
    UIButton *cancelButton = (UIButton *)[self.view viewWithTag:10009];
    cancelButton.hidden = YES;
}

#pragma mark 通知中心方法
- (void)receiveNotifica:(NSNotification *)center
{
    NSMutableDictionary *dic = [center.userInfo objectForKey:@"key"];
    // 给我的对象赋值（微博个人信息）
    self.mine = [[Mine alloc] initWithDictionary:dic];
//    NSLog(@"1111%@", self.mine.screen_name);
    
    
    
    // 赋值
    [self.weiboImageView setImageWithURL:[NSURL URLWithString:self.mine.avatar_large]];
    self.weiboName.text = self.mine.screen_name;
    self.attenNum.text = [self.mine.friends_count description];
    self.beanNum.text = [self.mine.followers_count description];
    self.weiboNum.text = [self.mine.statuses_count description];
    
    [self hide];
    
    
}
- (void)receiveNotifica1:(NSNotification *)center
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        
//        NSMutableDictionary *dic = [center.userInfo objectForKey:@"key"];
        // 给我的对象赋值（微博个人信息）
        self.mine = [[Mine alloc] initWithDictionary:center.userInfo];
        // 赋值
        [self.weiboImageView setImageWithURL:[NSURL URLWithString:self.mine.avatar_large]];
        self.weiboName.text = self.mine.screen_name;
        self.attenNum.text = [self.mine.friends_count description];
        self.beanNum.text = [self.mine.followers_count description];
        self.weiboNum.text = [self.mine.statuses_count description];
        
        NSLog(@"111111%@", self.mine.screen_name);
        
    });
    [self hide];
    
}

//   效果都在这个方法里写的
#pragma mark 拉伸效果方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat content = scrollView.contentOffset.y;
    CGRect newFrame = self.myImageView.frame;
    // 判断当前偏移状态
    if (content < 0) {
        // imageView的frame改变要小于偏移量
        newFrame.origin.y = IMAGEVIEW_ORIGIN_Y - content / 5;
        self.myImageView.frame = newFrame;
        
        
    } else if (content >= 0 && content < DETAIL_HIGHT) {
        // imageView的frame改变要等于偏移量
        newFrame.origin.y = IMAGEVIEW_ORIGIN_Y - content;
        self.myImageView.frame = newFrame;
    }
//    NSLog(@"图片起始点%f", newFrame.origin.y);
//    NSLog(@"偏移量%f", content);
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"我的收藏";
    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    //self.navigationController.navigationBarHidden = YES;
    self.navigationController.navigationBar.barTintColor = [UIColor ownColor];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil]];
#pragma mark 创建一个imageview
    // 创建一个imageview
    self.myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, IMAGEVIEW_ORIGIN_Y, self.view.frame.size.width, IMAGEVIEW_ORIGIN_HEITH)];
    self.myImageView.image = [UIImage imageNamed:@"Left.jpg"];
    [self.view addSubview:self.myImageView];
    
    // 登陆小人
    UIImageView *loginImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 2 - self.view.bounds.size.width * 0.13, self.view.bounds.size.height - self.view.bounds.size.height * 0.78, self.view.bounds.size.width * 0.28, self.view.bounds.size.height * 0.13)];
    //    loginImageView.backgroundColor = [UIColor yellowColor];
    [self.myImageView addSubview:loginImageView];
    self.myImageView.userInteractionEnabled = YES;
    loginImageView.image = [UIImage imageNamed:@"iconfont-xin-3.png"];
    loginImageView.userInteractionEnabled = YES;
    loginImageView.tag = 10001;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 2 - self.view.bounds.size.width * 0.86, self.view.bounds.size.height * 0.12, self.view.bounds.size.width, self.view.bounds.size.height * 0.08)];
    label.text = @"登陆";
    label.textColor = [UIColor whiteColor];
    label.textAlignment = 1;
    label.tag = 10002;
    //    label.backgroundColor = [UIColor redColor];
//    [loginImageView addSubview:label];
    [label release];
    [loginImageView release];
#pragma mark 登陆微博个人信息
    // 微博头像
    self.weiboImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 2 - self.view.bounds.size.width * 0.13, self.view.bounds.size.height - self.view.bounds.size.height * 0.78, self.view.bounds.size.width * 0.28, self.view.bounds.size.height * 0.13)];
    [self.myImageView addSubview:self.weiboImageView];
//    self.weiboImageView.backgroundColor = [UIColor redColor];
    self.weiboImageView.userInteractionEnabled = YES;
//    self.weiboImageView.tag = 10004;
    self.weiboImageView.hidden = YES;
    self.weiboImageView.clipsToBounds = YES;
    self.weiboImageView.layer.cornerRadius = 20;
    
    // 微博用户名
    self.weiboName = [[UILabel alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - self.view.bounds.size.height * 0.67, self.view.bounds.size.width, self.view.bounds.size.height * 0.08)];
//    NSLog(@"---------------%@", self.mine.screen_name);
    self.weiboName.textColor = [UIColor whiteColor];
    self.weiboName.textAlignment = 1;
//    self.weiboName.tag = 10005;
    self.weiboName.text = @"红尘梦最后的泪";
    [self.myImageView addSubview:self.weiboName];
    self.weiboName.hidden = YES;
    [_weiboName release];
    [_weiboImageView release];
    
    // 微博关注数
    UILabel *attenLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 2 - self.view.bounds.size.width * 0.36, self.view.bounds.size.height - self.view.bounds.size.height * 0.61, self.view.bounds.size.width * 0.18, self.view.bounds.size.height * 0.05)];
    attenLabel.text = @"关注";
    attenLabel.textColor = [UIColor whiteColor];
//    attenLabel.backgroundColor = [UIColor redColor];
    attenLabel.textAlignment = 1;
    attenLabel.tag = 10004;
    attenLabel.hidden = YES;
    [self.myImageView addSubview:attenLabel];
    [attenLabel release];
    
    self.attenNum = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 2 - self.view.bounds.size.width * 0.36, self.view.bounds.size.height - self.view.bounds.size.height * 0.57, self.view.bounds.size.width * 0.18, self.view.bounds.size.height * 0.05)];
    self.attenNum.text = @"123";
    self.attenNum.textColor = [UIColor whiteColor];
//    self.attenNum.backgroundColor = [UIColor redColor];
    self.attenNum.textAlignment = 1;
//    self.attenNum.tag = 10007;
    self.attenNum.hidden = YES;
    [self.myImageView addSubview:self.attenNum];
    [_attenNum release];
    
    
    // 微博粉丝
    UILabel *beanLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 2 - self.view.bounds.size.width * 0.08, self.view.bounds.size.height - self.view.bounds.size.height * 0.61, self.view.bounds.size.width * 0.18, self.view.bounds.size.height * 0.05)];
    beanLabel.text = @"粉丝";
    beanLabel.hidden = YES;
    beanLabel.textAlignment = 1;
    beanLabel.textColor = [UIColor whiteColor];
    beanLabel.tag = 10005;
//    beanLabel.font = [UIFont systemFontOfSize:16];
    [self.myImageView addSubview:beanLabel];
    [beanLabel release];
    
    self.beanNum = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 2 - self.view.bounds.size.width * 0.08, self.view.bounds.size.height - self.view.bounds.size.height * 0.57, self.view.bounds.size.width * 0.18, self.view.bounds.size.height * 0.05)];
    self.beanNum.textColor= [UIColor whiteColor];
    self.beanNum.text = @"456";
    self.beanNum.hidden = YES;
    self.beanNum.textAlignment = 1;
//    self.beanNum.tag = 10009;
    [self.myImageView addSubview:self.beanNum];
    [_beanNum release];
    
    // 微博数
    UILabel *wbLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 2 + self.view.bounds.size.width * 0.2, self.view.bounds.size.height - self.view.bounds.size.height * 0.61, self.view.bounds.size.width * 0.18, self.view.bounds.size.height * 0.05)];
    wbLabel.text = @"微博";
    wbLabel.textAlignment = 1;
    wbLabel.textColor = [UIColor whiteColor];
    wbLabel.tag = 10006;
    wbLabel.hidden = YES;
//    wbLabel.font = [UIFont systemFontOfSize:16];
    [self.myImageView addSubview:wbLabel];
    [wbLabel release];
    
    self.weiboNum = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 2 + self.view.bounds.size.width * 0.2, self.view.bounds.size.height - self.view.bounds.size.height * 0.57, self.view.bounds.size.width * 0.18, self.view.bounds.size.height * 0.05)];
    self.weiboNum.textColor= [UIColor whiteColor];
    self.weiboNum.text = @"789";
    self.weiboNum.textAlignment = 1;
//    self.weiboNum.tag = 10011;
    self.weiboNum.hidden = YES;
    [self.myImageView addSubview:self.weiboNum];
    [_weiboNum release];
    
    // 中间竖着的杠
    UILabel *shuGangLabelOne = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 2 - self.view.bounds.size.width * 0.215, self.view.bounds.size.height - self.view.bounds.size.height * 0.64, self.view.bounds.size.width * 0.18, self.view.bounds.size.height * 0.15)];
    shuGangLabelOne.textColor = [UIColor whiteColor];
    shuGangLabelOne.text = @"|";
    shuGangLabelOne.tag = 10007;
    shuGangLabelOne.hidden = YES;
    [self.myImageView addSubview:shuGangLabelOne];
    shuGangLabelOne.textAlignment = 1;
    shuGangLabelOne.font = [UIFont systemFontOfSize:30];
    [shuGangLabelOne release];
    
    UILabel *shuGangLabelTwo = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 2 + self.view.bounds.size.width * 0.07, self.view.bounds.size.height - self.view.bounds.size.height * 0.64, self.view.bounds.size.width * 0.18, self.view.bounds.size.height * 0.15)];
    shuGangLabelTwo.textColor = [UIColor whiteColor];
    shuGangLabelTwo.text = @"|";
    shuGangLabelTwo.hidden = YES;
    [self.myImageView addSubview:shuGangLabelTwo];
    shuGangLabelTwo.textAlignment = 1;
    shuGangLabelTwo.tag = 10008;
    shuGangLabelTwo.font = [UIFont systemFontOfSize:30];
    [shuGangLabelTwo release];
    
#pragma mark 创建tableview
    // 创建tableview
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, FINAL_HIGHT, self.view.frame.size.width, self.view.frame.size.height) style: UITableViewStylePlain];
    // 不显示滚动条
    self.myTableView.showsVerticalScrollIndicator = NO;
    self.myTableView.backgroundColor = [UIColor clearColor];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    
    
    NSArray *segArr = [NSMutableArray arrayWithObjects:@"送好礼", @"精选", @"分类", nil];
    
    //创建segment用来切换收藏
    self.seg = [[UISegmentedControl alloc] initWithItems:segArr];
    //    self.seg.frame = CGRectMake(10, 0, self.view.bounds.size.width, 10);
    self.seg.backgroundColor  = [UIColor whiteColor];
    self.seg.tintColor = [UIColor mypurpleColor];
    self.seg.selectedSegmentIndex = 1;
    [self.seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    [_seg release];
    
    
    
    
    
    
    // 用一个透明的view作为tableview的headerview
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, DETAIL_HIGHT - FINAL_HIGHT + 40)];
    // 把它设为no不会渲染它，效率略高点儿，不明显
    view.opaque = NO;
    
#pragma mark 创建登陆Button
    // 登陆按钮
    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
    loginButton.frame = CGRectMake(self.view.bounds.size.width / 2 - self.view.bounds.size.width * 0.125, self.view.bounds.size.height - self.view.bounds.size.height * 0.87, self.view.bounds.size.width * 0.27, self.view.bounds.size.height * 0.17);
    loginButton.backgroundColor = [UIColor clearColor];
    [loginButton addTarget:self action:@selector(loginButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
//    [view addSubview:loginButton];
    loginButton.tag = 10003;
    
    // 注销按钮
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
    cancelButton.frame = CGRectMake(self.view.bounds.size.width / 2 - self.view.bounds.size.width * 0.125, self.view.bounds.size.height - self.view.bounds.size.height * 0.87, self.view.bounds.size.width * 0.27, self.view.bounds.size.height * 0.17);
    cancelButton.backgroundColor = [UIColor clearColor];
    [cancelButton addTarget:self action:@selector(cancelButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
//    [view addSubview:cancelButton];
    cancelButton.tag = 10009;
    cancelButton.hidden = YES;
    
    
    //  self.table.contentInset = UIEdgeInsetsMake(240, 0, 0, 0);  //没有用这个方法是因为UITableViewStylePlain的section title 会停留在DETAIL_HIGHT的位置
    self.myTableView.tableHeaderView = view;
    
    [_myImageView release];
    [_myTableView release];
    
#pragma mark 创建rightBarButtonItem
    
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-shezhi.png"] style:UIBarButtonItemStylePlain target:self action:@selector(barButtonClicked:)] autorelease];
    
    
#pragma mark 没有收藏显示
        
    // 抽屉按钮
    [self setupLeftMenuButton];
    
    // Do any additional setup after loading the view.
}

#pragma mark 收藏切换
//点击segmentedControl时执行的方法
- (void)segAction:(UISegmentedControl *)seg
{
    //数据库单例
    DataBaseHandler *dataHandler = [DataBaseHandler shareInstance];
    
        if (seg.selectedSegmentIndex == 0) {
        //segment下标为0的时候执行
        //删除后覆盖的收藏label隐藏
        self.labelOne.hidden =YES;
        self.labelTwo.hidden = YES;
        self.labelThree.hidden = YES;
        [dataHandler  openDB];
            //得到数据库中bannerGift数组
        self.arr = [dataHandler selectBannerGift];
            //将全局变量置为0
        self.number = 0;
        [self.myTableView reloadData];
    }else if(seg.selectedSegmentIndex == 1){
        //segment下标为1的时候执行
        //删除后覆盖的收藏label隐藏
        self.labelOne.hidden =YES;
        self.labelTwo.hidden = YES;
        self.labelThree.hidden = YES;
        [dataHandler  openDB];
         //得到数据库中choiceness数组
        self.arr = [dataHandler selectChoiceness];
        self.number = 1;
        [self.myTableView reloadData];
    }else{
        //segment下标为2的时候执行
        //删除后覆盖的收藏label隐藏
        self.labelOne.hidden =YES;
        self.labelTwo.hidden = YES;
        self.labelThree.hidden = YES;
        [dataHandler  openDB];
        //得到数据库中Allgroup数组
        self.arr = [dataHandler selectAllGroup];
        self.number = 2;
        
        [self.myTableView reloadData];
    }
}



#pragma mark 抽屉按钮
// 设置navigation的左按钮为抽屉开关
- (void)setupLeftMenuButton
{
    MMDrawerBarButtonItem * leftDrawerButton = [[MMDrawerBarButtonItem alloc] initWithTarget:self action:@selector(leftDrawerButtonPress:)];
    [self.navigationItem setLeftBarButtonItem:leftDrawerButton animated:YES];
}
// 触发的方法
- (void)leftDrawerButtonPress:(id)sender{
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}

#pragma mark 登陆按钮
- (void)loginButtonClicked:(UIButton *)button
{
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    [self.navigationController pushViewController:loginVC animated:YES];
    [loginVC release];
}
#pragma mark 注销按钮
- (void)cancelButtonClicked:(UIButton *)button
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您确定要注销登陆吗" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alertView show];
    [alertView release];
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
//        NSLog(@"111");
        AppDelegate *myDelegate =(AppDelegate*)[[UIApplication sharedApplication] delegate];
        [WeiboSDK logOutWithToken:myDelegate.wbtoken delegate:self withTag:@"user1"];
        [self show];
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value",@"key", nil];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"tamenlaile2" object:@"香皂" userInfo:dic];
        self.myAlertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"注销成功" delegate:self cancelButtonTitle:nil otherButtonTitles: nil,nil];
        [self.myAlertView show];
        [NSTimer scheduledTimerWithTimeInterval:0.8f
                                         target:self
                                       selector:@selector(dismissAlert:)
                                       userInfo:nil
                                        repeats:NO];
        [self.myAlertView release];
    }
}
- (void)dismissAlert:(NSTimer *)timer{
    
    [self.myAlertView dismissWithClickedButtonIndex:0 animated:YES];
    
}

#pragma mark 设置按钮
- (void)barButtonClicked:(UIBarButtonItem *)item
{
    MoreViewController *moreVC = [[MoreViewController alloc] init];
//    UINavigationController *navigaMore = [[UINavigationController alloc] initWithRootViewController:moreVC];
//    moreVC.state = YES;
    [self.navigationController pushViewController:moreVC animated:YES];
    [moreVC release];
}


#pragma mark 设置cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.arr.count == 0) {
        return 1;
    }else{
    return self.arr.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    static NSString *indentifier = @"cell";
    
    if (self.number == 0) {
         //全局变量self.number为0时
        //cell显示bannerGift数据
        if (self.arr.count == 0) {
            NoCollectionCell *cell = [tableView dequeueReusableCellWithIdentifier: @"raa"];
            if (cell == nil) {
                cell = [[NoCollectionCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"raa"];
            }
            
           cell.collectLabel.text = @"您还没有送好礼收藏";
            self.myTableView.separatorStyle = NO;
            cell.userInteractionEnabled = NO;
            return cell;
        }else{
            MineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"rbb"];
            if (nil == cell) {
                cell = [[[MineCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"rbb"] autorelease];
            }
        BannerGift *bannerGift = [self.arr objectAtIndex:indexPath.row];
        cell.myLabel.text = bannerGift.title;
            [cell.myImageView setImageWithURL:[NSURL URLWithString:bannerGift.cover_image_url]];
            return cell;
        }
        
    }else if(self.number == 1){
        //全局变量self.number为1时
        //cell显示choiceness数据
        if (self.arr.count != 0) {
            MineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"rcc"];
            if (nil == cell) {
                cell = [[[MineCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"rcc"] autorelease];
            }
            
            Choiceness *choiceness = [self.arr objectAtIndex:indexPath.row];
            NSString *str = choiceness.cover_image_url;
            cell.myLabel.text = choiceness.title;
            [cell.myImageView setImageWithURL:[NSURL URLWithString:str]];
            return cell;

        }else{
            NoCollectionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"rdd"];

            if (cell == nil) {
                cell = [[NoCollectionCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"rdd"];
            }
            
            
            cell.collectLabel.text = @"您还没有精选收藏";
            self.myTableView.separatorStyle = NO;
            cell.userInteractionEnabled = NO;
            return cell;

        }
    }else {
        if (self.arr.count != 0) {
            MineCell *cell = [tableView dequeueReusableCellWithIdentifier: @"ree"];
            if (nil == cell) {
                cell = [[[MineCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"ree"] autorelease];
            }
            
            //全局变量self.number为2时
            //cell显示Allgroup数据
            AllGroup *group = [self.arr objectAtIndex:indexPath.row];
            cell.myLabel.text = group.title;
            [cell.myImageView setImageWithURL:[NSURL URLWithString:group.cover_image_url]];
            return cell;

        }else{
            NoCollectionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"rff"];
            
            if (cell == nil) {
                cell = [[NoCollectionCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"rff"];
            }
            cell.collectLabel.text = @"您还没有分类收藏";
            self.myTableView.separatorStyle = NO;
            cell.userInteractionEnabled = NO;
            return cell;
        }
    }
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.view.bounds.size.height / 3.0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{//点中cell选中条一闪而过
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //点击cell推送到对应的web上
    
    if (self.number == 0) {
        BannerGift *bannerGift = [self.arr objectAtIndex:indexPath.row];
        DetailsViewController *detVC = [[DetailsViewController alloc] init];
        detVC.banner = bannerGift;
        NSLog(@"%@",bannerGift.content_url);
        detVC.mark = 3;
        [self.navigationController pushViewController:detVC animated:YES];
    }else if (self.number == 1){
        Choiceness *choiceness = [self.arr objectAtIndex:indexPath.row];
        DetailsViewController *detVC = [[DetailsViewController alloc] init];
        detVC.choiceness = choiceness;
        
        [self.navigationController pushViewController:detVC animated:YES];
    }else{
        AllGroup *group = [self.arr objectAtIndex:indexPath.row];
        DetailsViewController *detVC = [[DetailsViewController alloc] init];
        detVC.mark = 2;
        detVC.group = group;
        
        
        [self.navigationController pushViewController:detVC animated:YES];
    }
    
    
    
    
    
}
//tableView数量
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

//tableView上分区的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.075 * self.view.bounds.size.height;
}

//返回分区显示的视图协议方法
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //创建一个数组放segment的标题
    return self.seg;
}


//- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return YES;
//}


//是否进入编辑状态

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    
    [super setEditing:editing animated:animated];
    [self.myTableView setEditing:editing animated:animated];
    
}
//指定tableView的编辑状态

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

//当点击删除按钮时，会调用这个协议方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"点击删除按钮");
    //需要对编辑方式行行判断
    if (editingStyle ==  UITableViewCellEditingStyleDelete) {
        //删除一个cell
        //删除之前，一定先对数据源进行处理
        if (self.number == 0) {
            //全局变量为对应数值0,1,2时删除对应活动
            DataBaseHandler *dataBaseHander = [DataBaseHandler shareInstance];
//            self.arr = [dataBaseHander selectBannerGift];
            BannerGift *banner = [self.arr objectAtIndex:indexPath.row];
            [dataBaseHander deleteBannerGiftWithTitle:banner.content_url];
            [self.arr removeObjectAtIndex:indexPath.row];
            
            
            [self.myTableView reloadData];
            
        }else if (self.number == 1){
            DataBaseHandler *dataBaseHander = [DataBaseHandler shareInstance];
//            self.arr = [dataBaseHander selectChoiceness];
            Choiceness *choiceness = [self.arr objectAtIndex:indexPath.row];
            [dataBaseHander deleteChoicenessWithTitle:choiceness.title];
            [self.arr removeObjectAtIndex:indexPath.row];
            [self.myTableView reloadData];
//            [self.myTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
            
        }else{
            DataBaseHandler *dataBaseHander = [DataBaseHandler shareInstance];
//            self.arr = [dataBaseHander selectAllGroup];
            AllGroup *group = [self.arr objectAtIndex:indexPath.row];
            [dataBaseHander deleteAllGroupWithTitle:group.title];
            [self.arr removeObjectAtIndex:indexPath.row];
            
            
            [self.myTableView reloadData];
           
            
        }
        
    }
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
